package lab8;

public class BinarySearch {

	public static int binarySearch(int[] array, int number, int minIndex, int maxIndex)
	{
		int middle = (maxIndex+minIndex)/2;
		if (array[middle]==number) return middle;
		if (array[middle]<number)
		{
			if (middle==maxIndex) return array.length;
			return binarySearch(array, number, middle+1, maxIndex);
		}
		else 
		{
			if (middle==minIndex) return array.length;
			return binarySearch(array, number, minIndex, middle-1);
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] array = {-4, 0, 3, 6, 7, 7, 12,19, 21};
		int number=21;
		int position = binarySearch(array, number, 0, array.length);
		if (position == array.length) System.out.println("Element not found");
		else System.out.println("Element found at index " + position+".");
	}

}
